//
//  BorrowViewController.m
//  TenThousand
//
//  Created by 王 on 16/7/16.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "WantBorrowViewController.h"
#import "BorrowCell.h"
#import "BorrowFooterView.h"

@interface WantBorrowViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableDictionary *dicM;

@end

@implementation WantBorrowViewController

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height) style:UITableViewStyleGrouped];
    }
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"BorrowCell" bundle:nil] forCellReuseIdentifier:@"BorrowCell"];
    _tableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    return _tableView;
}

- (NSMutableDictionary *)dicM {
    if (!_dicM) {
        _dicM = [NSMutableDictionary dictionary];
    }
    return _dicM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"我要借款";
    [self.view addSubview:self.tableView];
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:NO];
    } right:nil rightClick:nil];
    
    
}



#pragma mark - UITableView DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        BorrowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BorrowCell" forIndexPath:indexPath];
        if (indexPath.row == 1) {
            cell.imageV.image = [UIImage imageNamed:@"WYJKicon_09"];
            cell.textField.placeholder = @"手机号";
        }
       
      
        int  ll  = [[NSString stringWithFormat:@"%ld", (long)indexPath.section ] intValue]+10;
       NSString *kk   = [NSString stringWithFormat:@"%ld", (long)indexPath.row];
        cell.textField.tag = [[NSString stringWithFormat:@"%d%@",ll,kk] integerValue];
         NSLog(@"ttttt------==%ld",(long)cell.textField.tag);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        
        static NSString *Identifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
        }
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.textColor = [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1];
        cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"WYJKicon_18"]];
        if (indexPath.row == 0) {
            
            cell.imageView.image = [UIImage imageNamed:@"WYJKicon_11"];
            if (self.dicM[@"state"] == nil) {
                cell.textLabel.text = @"请选择标的状态";
            } else {
                cell.textLabel.text = self.dicM[@"state"];
                cell.textLabel.textColor = [UIColor blackColor];
            }
        } else {
            cell.imageView.image = [UIImage imageNamed:@"WYJKicon_20"];
            if (self.dicM[@"goods"] == nil) {
                cell.textLabel.text = @"请选择抵押物品";
            } else {
                cell.textLabel.text = self.dicM[@"goods"];
                cell.textLabel.textColor = [UIColor blackColor];
            }
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}


#pragma mark - UITableView Delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        
        
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"WYJKicon_13"]];
        
        NSMutableArray *arrayM = [NSMutableArray array];
        NSString *key;
        if (indexPath.row == 0) {
            
            NSArray *arr = @[@"已满标",@"招标中",@"审核中",@"还款中",@"已还完"];
            [arrayM addObjectsFromArray:arr];
            key = @"state";
        } else {
            
            NSArray *arr = @[@"房子",@"汽车",@"手机",@"手表",@"公司"];
            [arrayM addObjectsFromArray:arr];
            key = @"goods";
        }
        BXPickerView *pickerView = [[BXPickerView alloc] initWithFrame:CGRectMake(0, Screen_Height-220, Screen_Width, 220) WithTitleListArr:arrayM WithIndex:1];
        pickerView.clickedBtnIndexBlock = ^(NSString *text, NSInteger index){
            
            [self.dicM setValue:text forKey:key];
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        };
        
        [pickerView showView];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return 90;
    } else {
        return 0.01;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return [UIView new];
    }
    BorrowFooterView *borrowFooterV = [[BorrowFooterView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 90)];
    [borrowFooterV.applyForBtn addTarget:self action:@selector(applyForBtnClcik:) forControlEvents:UIControlEventTouchUpInside];
    return borrowFooterV;
}

//立即申请
-(void)applyForBtnClcik:(UIButton *)btn
{
    UITextView *nameTf = [self.tableView viewWithTag:100];
    UITextView *phoneTf = [self.tableView viewWithTag:101];

    NSString *dic;
    if(self.dicM.count){
        dic = self.dicM[@"goods"];
    }
    
    if ([nameTf.text isEqualToString:@""]) {
        [ToastView showInfoWithStatus:@"姓名不能为空"];
    }else if ([phoneTf.text isEqualToString:@""]) {
        [ToastView showInfoWithStatus:@"手机号码不能为空"];
    }else if (![PhoneNumStandard isStandardOrNotWithPhoneNumber:phoneTf.text]) {
        [ToastView showInfoWithStatus:@"手机号输入不合法，请重新输入"];
    }else if ([dic isEqualToString:@""]){
        [ToastView showInfoWithStatus:@"抵押物品不能为空"];
    }else{
    [self getMyBrrow];
    }
}
//我要借款接口
-(void)getMyBrrow{
    
    UITextView *nameTf = [self.tableView viewWithTag:100];
    UITextView *phoneTf = [self.tableView viewWithTag:101];
    NSLog(@"手机号---%@--%@-- %@",nameTf.text,phoneTf.text,self.dicM[@"goods"]);
    
    if(self.dicM.count){
        NSDictionary *para = @{
                               @"RealName":nameTf.text,
                               @"PhoneNumber":phoneTf.text,
                               @"Collateral":self.dicM[@"goods"]
                               };
        
        [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Borrow.GetMyBrrow" withParaments:para withSuccessBlock:^(NSDictionary *object) {
            NSLog(@"%@",object);
            [ToastView showInfoWithStatus:object[@"Message"]];
//            if ([object[@"Code"] isEqualToString:@"00000"]) {
//                [self.navigationController popViewControllerAnimated:YES];
//            }
        } withFailureBlock:^(NSError *error) {
            NSLog(@"%@",error);
        } progress:nil];

    }
    
    
    
   
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
